#pragma once
#include "common.h"
class HandelSerDser
{
    public:
    virtual std::string serialize() const = 0;
    virtual void deserialize(const std::string &json_str) = 0;
};
// 学生类定义
class Student : public HandelSerDser
{
public:
    std::string name;
    std::string student_id;
    std::string class_name;
    std::string gender;
    int age;
    std::string birth_date;
    int student_number;
    int college_number;
    float score;

    // 序列化为 JSON 字符串
    std::string serialize() const
    {
        std::ostringstream oss;
        oss << "{";
        oss << "\"姓名\":\"" << name << "\",";
        oss << "\"学号\":\"" << student_id << "\",";
        oss << "\"所在班级\":\"" << class_name << "\",";
        oss << "\"性别\":\"" << gender << "\",";
        oss << "\"年龄\":" << age << ",";
        oss << "\"出生年日\":\"" << birth_date << "\",";
        oss << "\"学生编号\":" << student_number << ",";
        oss << "\"学院编号\":" << college_number << ",";
        oss << "\"成绩\":" << score;
        oss << "}";
        return oss.str();
    }
    std::string strTosql() const
    {
        std::ostringstream oss;
        oss << "(";
        oss << "\"" << name << "\",";
        oss << "\"" << student_id<< "\",";
        oss << "\"" << class_name << "\",";
        oss << "\"" << gender << "\",";
        oss << age << ",";
        oss << "\"" << birth_date << "\",";
        oss << student_number << ",";
        oss << college_number << ",";
        oss << score;
        oss << ");";
        return oss.str();
    }
    // 从 JSON 反序列化
    void deserialize(const std::string &json_str)
    {
        // 简单的 JSON 解析
        std::istringstream iss(json_str);
        std::vector<std::string> tokens;
        std::string token;

        while (std::getline(iss, token, ','))
        {
            tokens.push_back(token);
        }

        for (const auto &token : tokens)
        {
            size_t colon_pos = token.find(':');
            if (colon_pos != std::string::npos)
            {
                std::string key = token.substr(1, colon_pos - 2);                        // 去除引号的键
                std::string value = token.substr(colon_pos + 1);                         // 值
                value.erase(std::remove(value.begin(), value.end(), '\"'), value.end()); // 去掉值的引号

                if (key == "姓名")
                {
                    name = value;
                }
                else if (key == "学号")
                {
                    student_id = value;
                }
                else if (key == "所在班级")
                {
                    class_name = value;
                }
                else if (key == "性别")
                {
                    gender = value;
                }
                else if (key == "年龄")
                {
                    age = std::stoi(value);
                }
                else if (key == "出生年日")
                {
                    birth_date = value;
                }
                else if (key == "学生编号")
                {
                    student_number = std::stoi(value);
                }
                else if (key == "学院编号")
                {
                    college_number = std::stoi(value);
                }
                else if (key == "成绩")
                {
                    score = std::stof(value);
                }
            }
        }
    }
};
// 教师类定义
class Teacher : public HandelSerDser
{
public:
    std::string name;
    std::string class_managed; // 管理班级
    std::string gender;
    int age;
    std::string birth_date;
    int teacher_number;

    // 序列化为 JSON 字符串
    std::string serialize() const
    {
        std::ostringstream oss;
        oss << "{";
        oss << "\"姓名\":\"" << name << "\",";
        oss << "\"管理班级\":\"" << class_managed << "\",";
        oss << "\"性别\":\"" << gender << "\",";
        oss << "\"年龄\":" << age << ",";
        oss << "\"出生年日\":\"" << birth_date << "\",";
        oss << "\"教师编号\":" << teacher_number;
        oss << "}";
        return oss.str();
    }

    // 从 JSON 反序列化
    void deserialize(const std::string &json_str)
    {
        // 简单的 JSON 解析
        std::istringstream iss(json_str);
        std::vector<std::string> tokens;
        std::string token;

        while (std::getline(iss, token, ','))
        {
            tokens.push_back(token);
        }

        for (const auto &token : tokens)
        {
            size_t colon_pos = token.find(':');
            if (colon_pos != std::string::npos)
            {
                std::string key = token.substr(1, colon_pos - 2);                        // 去除引号的键
                std::string value = token.substr(colon_pos + 1);                         // 值
                value.erase(std::remove(value.begin(), value.end(), '\"'), value.end()); // 去掉值的引号

                if (key == "姓名")
                {
                    name = value;
                }
                else if (key == "管理班级")
                {
                    class_managed = value;
                }
                else if (key == "性别")
                {
                    gender = value;
                }
                else if (key == "年龄")
                {
                    age = std::stoi(value);
                }
                else if (key == "出生年日")
                {
                    birth_date = value;
                }
                else if (key == "教师编号")
                {
                    teacher_number = std::stoi(value);
                }
            }
        }
    }
};
// 专业学院
class College : public HandelSerDser
{
public:
    int college_number;
    std::string college_name;
    int course_number;

    // 序列化为 JSON 字符串
    std::string serialize() const
    {
        std::ostringstream oss;
        oss << "{";
        oss << "\"学院编号\":" << college_number << ",";
        oss << "\"学院名称\":\"" << college_name << "\",";
        oss << "\"课程数量\":" << course_number;
        oss << "}";
        return oss.str();
    }

    // 从 JSON 反序列化
    void deserialize(const std::string &json_str)
    {
        std::istringstream iss(json_str);
        std::vector<std::string> tokens;
        std::string token;

        while (std::getline(iss, token, ','))
            tokens.push_back(token);

        for (const auto &token : tokens)
        {
            size_t colon_pos = token.find(':');
            if (colon_pos != std::string::npos)
            {
                std::string key = token.substr(1, colon_pos - 2);
                std::string value = token.substr(colon_pos + 1);
                value.erase(std::remove(value.begin(), value.end(), '\"'), value.end());

                if (key == "学院编号")
                {
                    college_number = std::stoi(value);
                }
                else if (key == "学院名称")
                {
                    college_name = value;
                }
                else if (key == "课程数量")
                {
                    course_number = std::stoi(value);
                }
            }
        }
    }
};
// 课程
class Course : public HandelSerDser
{
public:
    int course_number;
    std::string course_name;

    // 序列化为 JSON 字符串
    std::string serialize() const
    {
        std::ostringstream oss;
        oss << "{";
        oss << "\"课程编号\":" << course_number << ",";
        oss << "\"课程名称\":\"" << course_name << "\"";
        oss << "}";
        return oss.str();
    }

    // 从 JSON 反序列化
    void deserialize(const std::string &json_str)
    {
        std::istringstream iss(json_str);
        std::vector<std::string> tokens;
        std::string token;

        while (std::getline(iss, token, ','))
            tokens.push_back(token);

        for (const auto &token : tokens)
        {
            size_t colon_pos = token.find(':');
            if (colon_pos != std::string::npos)
            {
                std::string key = token.substr(1, colon_pos - 2);
                std::string value = token.substr(colon_pos + 1);
                value.erase(std::remove(value.begin(), value.end(), '\"'), value.end());

                if (key == "课程编号")
                {
                    course_number = std::stoi(value);
                }
                else if (key == "课程名称")
                {
                    course_name = value;
                }
            }
        }
    }
};
// 管理
class Admin : public HandelSerDser
{
public:
    int admin_number;
    std::string name;
    std::string gender;

    // 序列化为 JSON 字符串
    std::string serialize() const
    {
        std::ostringstream oss;
        oss << "{";
        oss << "\"管理员编号\":" << admin_number << ",";
        oss << "\"姓名\":\"" << name << "\",";
        oss << "\"性别\":\"" << gender << "\"";
        oss << "}";
        return oss.str();
    }

    // 从 JSON 反序列化
    void deserialize(const std::string &json_str)
    {
        std::istringstream iss(json_str);
        std::vector<std::string> tokens;
        std::string token;

        while (std::getline(iss, token, ','))
            tokens.push_back(token);

        for (const auto &token : tokens)
        {
            size_t colon_pos = token.find(':');
            if (colon_pos != std::string::npos)
            {
                std::string key = token.substr(1, colon_pos - 2);
                std::string value = token.substr(colon_pos + 1);
                value.erase(std::remove(value.begin(), value.end(), '\"'), value.end());

                if (key == "管理员编号")
                {
                    admin_number = std::stoi(value);
                }
                else if (key == "姓名")
                {
                    name = value;
                }
                else if (key == "性别")
                {
                    gender = value;
                }
            }
        }
    }
};
